﻿module Lib.Set ((⊂), (⊃), (⊆), (⊇), (∩), (∪), (\\), (∈), (∋), (===)) where

import Lib.Logic

-- = Operátorok = --

infix  4 ⊂, ⊃, ⊆, ⊇
infixr 3 ∩
infixr 2 ∪
infixl 3 \\
--infix? ? ∈
--infix? ? ∋

--operátorok definíciói

x ∈ xs = elem x xs
xs ∋ x = flip (∈)

(∩) a b  = intersection a b
(∪) a b  = union a b
(\\) a b  = difference a b

(⊂) a b  = isProperSubsetOf a b
(⊃) a b  = flip (⊂)
(⊆) a b  = isSubsetOf a b
(⊇) a b  = flip (⊆)

a === b = (isSubsetOf a b) && (isSubsetOf b a)

-- = Fő függvénydefiníciók = --

--a metszet b

intersection :: (Eq a) => [a] -> [a] -> [a]
intersection a b = [x | x <- a, x ∈ b]
-- Magyrázat:
--   értsd: a ∩ b = [x ∈ a | x ∈ b]

--a b-re vett különbsége

difference :: (Eq a) => [a] -> [a] -> [a]
difference a b = [x | x <- a, not (x ∈ b) ]

--szimmetrikus differencia

symmDifference :: (Eq a) => [a] -> [a] -> [a]
symmDifference a b = (a \\ b) ++ (b \\ a)

--a unió b

union :: (Eq a) => [a] -> [a] -> [a]
union a b = (symmDifference a b) ++ (a ∩ b)
-- TODO: egyéb ötlet?

--a részhalmaza b-nek

isSubsetOf :: (Eq a) => [a] -> [a] -> Bool
isSubsetOf a b = all (`elem` b) a

--a valódi részhalmaza b-nek

isProperSubsetOf :: (Eq a) => [a] -> [a] -> Bool
isProperSubsetOf a b = a /= b && isSubsetOf a b